package cn.com.huzo.webapp.controller.master;

import cn.com.huzo.model.entities.master.EndUserCatalogueEntity;
import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.EndUserEntity;
import cn.com.huzo.service.master.EndUserCatalogueManager;
import cn.com.huzo.service.master.EndUserManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 终端客户控制器
 *
 * @author 吴旭
 */
@Controller
public class EndUserController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">    
    @Resource(name = "EndUserManager")
    private EndUserManager _manager;

    @Resource(name = "EndUserCatalogueManager")
    private EndUserCatalogueManager _managerCatalogue;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到终端客户一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/enduser/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.enduser.list.def");
        return view;
    }

    /**
     * 终端客户编辑画面
     * <p>
     * @param session
     * @param action
     * @param enduserId
     * @param nodeId
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enduser/{action}/{enduserId}/{nodeId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpSession session, @PathVariable(value = "action") int action,
            @PathVariable(value = "enduserId") Long enduserId,
            @PathVariable(value = "nodeId") Long nodeId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("enduserId", enduserId);

        if (nodeId != 0L) {
            EndUserCatalogueEntity _item = _managerCatalogue.fetchByKey(nodeId);
            view.addObject("nodeId", nodeId);
            view.addObject("nodeText", _item.getParentName() + " → " + _item.getCatalogueName());
        }

        view.setViewName("master.enduser.edit.def");
        return view;
    }

    /**
     * 弹出终端客户选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/enduser/choice.html")
    public ModelAndView redirectChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.enduser.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据标识取得终端客户数据。
     *
     * @param criterion 终端客户标识、当前用户、流程标识
     * @return 终端客户数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enduser/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    EndUserEntity fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enduser/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 根据条件取得终端客户分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 终端客户分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/enduser/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 保存终端客户数据。
     * <p>
     * @param instance 要保存的终端客户对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/enduser/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody EndUserEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getEndUserId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance);
        } else {//新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 批量更新终端客户所属目录。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/enduser/batchChangeCatalogue.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer batchChangeCatalogue(@RequestBody final Map<String, ? extends List<EndUserEntity>> itemList) throws Exception {
        int rowsAffected = 0;
        if (itemList.get("updateList").size() > 0) {
            rowsAffected = _manager.batchChangeCatalogue(itemList.get("updateList"));
        }

        return rowsAffected;
    }
    // </editor-fold>
}
